package cn.t.service.order;

import cn.t.constants.CommonConstants;
import cn.t.constants.ParamConstants;
import cn.t.core.mode.PageQueryDTO;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.service.ServiceImpl;
import cn.t.dto.order.QuotationQueryRequest;
import cn.t.model.order.OrderDO;
import cn.t.model.order.OrderDetailDO;
import cn.t.model.order.QuotationLineDO;
import cn.t.repository.order.OrderDetailRepository;
import cn.t.utils.DateUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 领料订单明细表(OrderDetail)领域服务
 *
 * @author t
 * @since 2024-05-24 10:19:12
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrderDetailService extends ServiceImpl<OrderDetailDO, OrderDetailRepository> {
  public Boolean saveOrUpdateBatch(List<OrderDetailDO> orderDetailDOList){
      return   repository.addOrUpdateBatch(orderDetailDOList);

  }
    public Boolean  deleteOrderDetail(OrderDetailDO template,List<Long> ids) {
              template.setIsDeleted(YesNoEnum.TRUE);
      return   repository.updateModel(template,ids);
    }
  public  List<OrderDetailDO> queryListByOrderSn(OrderDetailDO orderDetailDO) {
    return findByModel(orderDetailDO, CommonConstants.ZERO, Integer.MAX_VALUE, null, null);

  }
  public Long queryOrderDetailCount( QuotationQueryRequest param){
    List<OrderDO> orderBeanList = orderService.orderQuoteOrders(param);
    if (ObjectUtils.isEmpty(orderBeanList)) {
      return 0L;
    }
    List<String> orderSnList = orderBeanList.stream().map(OrderDO::getOrderSn).collect(Collectors.toList());
    return   repository.queryOrderDetailCount(param,orderSnList);
  }
  public List<OrderDetailDO> queryDetailByUserFirm(Long userId, Long firmId, List<String> skuCodes, Date timeStart){
        return repository.queryDetailByUserFirm(userId,firmId,skuCodes,timeStart);
  }
  public List<QuotationLineDO> queryOrderDetailList(PageQueryDTO<QuotationQueryRequest> pageQueryDTO) {
    QuotationQueryRequest param = pageQueryDTO.getParam();
    List<OrderDO> orderBeanList = orderService.orderQuoteOrders(param);
    if (ObjectUtils.isEmpty(orderBeanList)) {
        return Collections.emptyList();
    }
    List<String> orderSnList = orderBeanList.stream().map(OrderDO::getOrderSn).collect(Collectors.toList());
    //查询订单行信息
    List<OrderDetailDO> orderDetailDOS = repository.queryOrderDetailList(param,orderSnList , pageQueryDTO.getStart(), pageQueryDTO.getPageSize(), " id", "desc");
    if (ObjectUtils.isEmpty(orderDetailDOS)) {
      return Collections.emptyList();
    }
    Map<String, OrderDO> orderMap = orderBeanList.stream().collect(Collectors.toMap(OrderDO::getOrderSn, orderDO -> orderDO, (k1, k2) -> k1));
   return orderDetailDOS.stream().map(orderDetailDO -> convertBeanToDetail(orderDetailDO, orderMap.get(orderDetailDO.getOrderSn()))).collect(Collectors.toList());

  }
  public List<QuotationLineDO> getAllSync(QuotationQueryRequest request) {
    PageQueryDTO<QuotationQueryRequest> requestPageQuery=new PageQueryDTO<>();
    requestPageQuery.setParam(request);
    requestPageQuery.setPageNo(ParamConstants.INT_0);
    requestPageQuery.setPageSize(ParamConstants.ORDER_PAGE_MAX_SIZE);
    return  queryOrderDetailList(requestPageQuery);
  }
  public  QuotationLineDO convertBeanToDetail(OrderDetailDO detailDO, OrderDO orderDO) {
    QuotationLineDO resp = new QuotationLineDO();
    if (orderDO!=null){
      resp.setDeviceId(Math.toIntExact(orderDO.getDeviceId()));
      resp.setOrderSn(orderDO.getOrderSn());
      resp.setCostCenterName(orderDO.getCostCenterName());
      resp.setUserName(orderDO.getUserName());
      resp.setDeviceName(orderDO.getDeviceName());
      resp.setGmtCreatOrder(DateUtil.setTime(orderDO.getGmtCreated()));
    }
    resp.setSid(detailDO.getId());
    resp.setSkuName(detailDO.getSkuName());
    resp.setSkuCode(detailDO.getSkuCode());

    resp.setQty(detailDO.getQty());
    resp.setCargowayNo(detailDO.getCargowayNo());
    return resp;
  }
  @Resource
  private OrderService orderService;
}
